package simulator.ui;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;

import javax.swing.JComponent;

import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.renderer.category.BarRenderer;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.ui.HorizontalAlignment;
import org.jfree.ui.RectangleEdge;
import org.jfree.ui.RectangleInsets;
import org.jfree.ui.VerticalAlignment;

import simulator.Scenario;
import simulator.software.Process;

/**
 * Charting component that paints a bar chart depicting total wait time by
 * process.
 * 
 * @author Kyle Loomis
 * 
 */
public class ProcessWaitChart extends JComponent
{
   private static final long serialVersionUID = 1L;
   private Scenario scenario = null;
   private DefaultCategoryDataset dataset = null;
   private JFreeChart utilChart = null;

   /**
    * Default constructor.
    * 
    * @param scenario
    *           the scenario on which the chart is based.
    */
   public ProcessWaitChart(Scenario scenario)
   {
      this.scenario = scenario;

      dataset = new DefaultCategoryDataset();

      Font normal = new Font("Arial", Font.PLAIN, 12);

      Color[] processColors = new Color[10];

      processColors[1] = new Color(220, 220, 220);
      processColors[1] = new Color(205, 255, 205);
      processColors[2] = new Color(205, 205, 255);
      processColors[3] = new Color(255, 255, 205);
      processColors[4] = new Color(255, 205, 255);
      processColors[5] = new Color(205, 255, 255);
      processColors[6] = new Color(205, 205, 205);
      processColors[7] = new Color(255, 205, 170);
      processColors[8] = new Color(255, 170, 205);
      processColors[9] = new Color(170, 205, 255);

      BarRenderer renderer = new BarRenderer();

      CategoryAxis catAxis = new CategoryAxis();
      NumberAxis numAxis = new NumberAxis();
      numAxis.setLabelPaint(new Color(100, 100, 100));

      CategoryPlot waitPlot = new CategoryPlot(dataset, catAxis, numAxis,
            renderer);

      int index = 0;

      for (Process p : scenario.getComputer().getKernel().getProcesses())
      {
         dataset.setValue(p.getCyclesWaiting(), "Cycles", p.getName());
         index++;
      }

      utilChart = new JFreeChart(waitPlot);
      utilChart.setBackgroundPaint(Color.white);
      utilChart.setAntiAlias(true);
      utilChart.setBorderVisible(false);
      utilChart.setPadding(new RectangleInsets(0.0, 0.0, 0.0, 0.0));
      utilChart.setTitle(new TextTitle("Process Wait (Cycles)", normal,
            new Color(100, 100, 100), RectangleEdge.TOP,
            HorizontalAlignment.LEFT, VerticalAlignment.BOTTOM,
            new RectangleInsets(0.0, 10.0, 0.0, 0.0)));

      utilChart.removeLegend();
   }

   public Dimension getMinimumSize()
   {
      return new Dimension(350, 200);
   }

   public void paintComponent(Graphics g)
   {
      super.paintComponent(g);

      Graphics2D area = (Graphics2D) g;

      for (Process p : scenario.getComputer().getKernel().getProcesses())
      {
         dataset.setValue(p.getCyclesWaiting(), "Cycles", p.getName());
      }

      utilChart.draw(area, area.getClipBounds());
   }

}
